<?php

class PhotoController extends Zend_Controller_Action
{
    public function init()
    {
    	
    }

    //http://localhost/trombino/public/index.php/photo/list
    public function listAction()
    {  	
    	$this->view->data = $this->getPhotos();    	
    }
    
    public function detailAction()
    {
    	//$this->view->data = $this->getRequest()->getPost('id'); 
    	$id = $this->getRequest()->getParam('id');   	
    	
    	$photos = $this->getPhotos();
    	foreach($photos as $key=>$value)
    	{
    		if($id == $key)
    		{
    			$this->view->data = $value;
    			break;
    		}    		
    	}
    }
    
    public function activeAction()
    {
    	$this->InitActive();
    	
    	$id = $this->getRequest()->getParam('id');
    	$statut = $this->getRequest()->getParam('statut');
    	$date = date('Y-m-j');  
    	
    	$dbUser = new Model_DbTable_Flux_Uti();
    	$user = $dbUser->findByLogin($id);    	
    	
    	$uid = 0;
    	if($user == null)
    	{
    		$uid = $dbUser->ajouter(array("login"=>$id, "maj"=>'', "flux"=>'',
    					"mdp"=>'', "role"=>'', "mdp_sel"=>'',
    					"email"=>'', "ip_inscription"=>'', "date_inscription"=>$date
    				));
    	}
    	else
    	{
    		$uid = $user['uti_id'];	
    	}
    	
    	if($uid != 0)
    	{
	    	$db = new Model_DbTable_flux_actiuti();
	    	$db->ajouter(array("acti_id"=>$statut, "uti_id"=>$uid,
	    			"date"=>$date
	    			));
    	}
    	
    	$this->_redirect ('/photo/list');
    }
    
    public function getPhotos()
    {  
    	$photos = "";
    	$url = 'https://picasaweb.google.com/data/feed/base/user/113848708930851956597/albumid/5795380358275112865?alt=rss&kind=photo&hl=fr';
    	$xml = simplexml_load_file($url);
    	 
    	foreach ($xml->channel->item as $item){  	
    		//echo $item->enclosure['url'].' | '.$item->title.'<br/>';
    		 
    		//link: https://picasaweb.google.com/113848708930851956597/Trombino#5795380520122136130
    		$links = explode("#", $item->link);
    		if(!empty($links))
    		{
    			$id = $links[1];
    			$photos[$id] = $item->enclosure['url'].'|'.$this->getUserActive($id); 
    		}   	
    	}
    	
    	return $photos;
    }
    
    public function getUserActive($login)
    {
    	$active = '';
    	
    	$dbUser = new Model_DbTable_Flux_Uti();
    	$user = $dbUser->findByLogin($login);
    	
    	if($user != null)
    	{    	
    		$uid = $user['uti_id'];
    		
	    	$dbActiuti = new Model_DbTable_flux_actiuti();
	    	$actiuti = $dbActiuti->findByUti_id($uid);
	    	
	    	if($actiuti != null)
	    	{
	    		$actiID = $actiuti[0]['acti_id'];
	    		$dbActi = new Model_DbTable_flux_acti();
		    	$acti = $dbActi->findByActi_id($actiID);
		    	 
		    	if($acti != null)
		    	{
		    		$active = $acti[0]['code'];
		    	}
	    	}
    	}
    	
    	return $active;
    }
    
    public function InitActive()
    {
    	$db = new Model_DbTable_flux_acti();
    	$acts = $db->getAll();
    	
    	if($acts == null)
    	{
    		$db->ajouter(array("acti_id"=>1, "code"=>'Presente'));
    		$db->ajouter(array("acti_id"=>2, "code"=>'Noraml'));
    		$db->ajouter(array("acti_id"=>3, "code"=>'Absente'));    		
    	} 
    }
}